package daoimp;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.JDBCTemplate;
import dao.UserDao;
import entity.User;

public class UserDaoImp implements UserDao
{
    JDBCTemplate jt;
    
    public UserDaoImp()
    {
        jt = new JDBCTemplate();
    }
	public int add(User u) throws SQLException {
		return jt.executeupdate("insert into user(uname,upwd,authority) values(?,?,?)",u.getUname(),u.getUpwd(),u.getAuthority());
		   
	}
    
    public int delete(int uid)
        throws SQLException
    {
        return jt.executeupdate("delete from user where uid=?", uid);
    }
    
    public int update(User u)
        throws SQLException
    {
        return jt.executeupdate("update user set uname=?,upwd=?,authority=?",
            u.getUname(),
            u.getUpwd(), u.getAuthority());
    }
    public List query()
        throws SQLException
    {
        return jt.executequery("select*from user", User.class, null);
    }
    
    //根据姓名模糊查询
    public List que(String name)
        throws SQLException
    {
        return jt.executequery("select * from user where uname like ?", User.class, "%"+name+"%");
    }

	public User selectName(String name) throws SQLException {
		 	List<User> list = new ArrayList();
	        list = jt.executequery("select*from user where uname=?", User.class, name);
	        return list.size() > 0 ? list.get(0) : null;
	}


}
